/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.transitionseverywhere.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.utils.LightweightMap;

public class InterPolatorSample extends AbilitySlice {
    private static final int ID_BUTTON = 200;
    private AdaptiveBoxLayout mStackLayout = new AdaptiveBoxLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer.LayoutConfig mLayoutConfig = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT);
        mStackLayout.setLayoutConfig(mLayoutConfig);
        mStackLayout.setId(111);
        Object tagMap = new LightweightMap<>();
        mStackLayout.setTag(tagMap);
        ShapeElement element = new ShapeElement();
        element.setShape(ShapeElement.RECTANGLE);
        element.setRgbColor(new RgbColor(255, 255, 255));
        mStackLayout.setBackground(element);
        ComponentContainer.LayoutConfig mbutton = new ComponentContainer.LayoutConfig();
        mbutton.setMargins(10,50,10,10);
        Button button;
        button = new Button(this);
        button.setLayoutConfig(mbutton);
        button.setText("Press To Slide");
        button.setTextSize(100);
        button.setId(ID_BUTTON);
        mStackLayout.addComponent(button);
        ShapeElement background1 = new ShapeElement();
        background1.setRgbColor(new RgbColor(0xFF0000DD));
        Color col1=Color.BLUE;
        button.setBackground(background1);
        button.setTextColor(col1);
        setUIContent(mStackLayout);

        button.setClickedListener(new Component.ClickedListener() {
            boolean mToRightAnimation;
            @Override
            public void onClick(Component Component) {
                mToRightAnimation = !mToRightAnimation;
                Transition transition = new ChangeBounds();
                TransitionManager.beginDelayedTransition(mStackLayout, transition);
                AdaptiveBoxLayout.LayoutConfig params= (AdaptiveBoxLayout.LayoutConfig) button.getLayoutConfig();
                params.alignment = mToRightAnimation ? (Gravity.RIGHT | Gravity.TOP) : (Gravity.LEFT | Gravity.TOP);
                button.setLayoutConfig(params);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
